package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.LayR;
import com.gxa.common.R;
import com.gxa.mapper.DeptMapper;
import com.gxa.mapper.NoticeDeptMapper;
import com.gxa.mapper.NoticeMapper;
import com.gxa.param.NoticeParam;
import com.gxa.pojo.Dept;
import com.gxa.pojo.Notice;
import com.gxa.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;


@Service
@Transactional(rollbackFor = Throwable.class)
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper mapper;
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public LayR list(NoticeParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<Notice> notices= mapper.findAll(param);
        PageInfo<Notice> pageInfo=new PageInfo<>(notices);
        List<Notice> list= pageInfo.getList();
        for(Notice notice:list){
            System.out.println(notice);
        }
        return new LayR(0, "success", pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public LayR changeStatus(Notice notice) {
        mapper.update(notice);
        return new LayR(200, "success");
    }

    @Override
    public R add(Notice notice) {

        Dept dept = deptMapper.findByPk(notice.getDeptId());
        notice.setPublishName(dept.getDeptName());
        notice.setPublishTime(new Timestamp(System.currentTimeMillis()));

        mapper.save(notice);
        return new R(200,"success");
    }


    @Override
    public R deleteAll(String[] ids) {
        mapper.deleteAll(ids);
        return new R(200, "删除成功！");
    }

    @Override
    public R deletid(Integer noticeId) {
        mapper.deleteid(noticeId);
        return new R(200,"删除成功");
    }

    @Override
    public R details(Integer noticeId) {
        Notice notice = mapper.findByPK(noticeId);
        System.out.println(notice);
        return new R(200,"success",notice);
    }


}
